package org.jeecg.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.oss.OssBootUtil;
import org.jeecg.modules.wms.entity.WmsRequisitionAttachment;
import org.jeecg.modules.wms.mapper.WmsRequisitionAttachmentMapper;
import org.jeecg.modules.wms.service.IWmsRequisitionAttachmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 领料单附件服务实现类
 */
@Slf4j
@Service
public class WmsRequisitionAttachmentServiceImpl extends ServiceImpl<WmsRequisitionAttachmentMapper, WmsRequisitionAttachment> implements IWmsRequisitionAttachmentService {

    @Override
    @Transactional
    public Result<WmsRequisitionAttachment> uploadAttachment(String requisitionId, MultipartFile file, String uploader) {
        try {
            // 上传文件到OSS
            String fileName = file.getOriginalFilename();
            String suffix = FilenameUtils.getExtension(fileName);
            String url = OssBootUtil.upload(file, "wms/requisition/attachment");
            
            // 创建附件记录
            WmsRequisitionAttachment attachment = new WmsRequisitionAttachment();
            attachment.setRequisitionId(requisitionId);
            attachment.setFileName(fileName);
            attachment.setFilePath(url);
            attachment.setFileType(suffix);
            attachment.setFileSize(file.getSize());
            attachment.setUploadTime(new Date());
            attachment.setUploader(uploader);
            attachment.setCreateBy(uploader);
            attachment.setCreateTime(new Date());
            
            // 保存附件记录
            this.save(attachment);
            
            return Result.OK("上传成功", attachment);
        } catch (Exception e) {
            log.error("上传附件失败", e);
            return Result.error("上传附件失败: " + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<Boolean> deleteAttachment(String id) {
        try {
            // 获取附件信息
            WmsRequisitionAttachment attachment = this.getById(id);
            if (attachment == null) {
                return Result.error("附件不存在");
            }
            
            // 从OSS删除文件
            String filePath = attachment.getFilePath();
            if (filePath != null && filePath.startsWith("http")) {
                String objectName = CommonUtils.getFileName(filePath);
                OssBootUtil.deleteUrl(objectName);
            }
            
            // 删除附件记录
            this.removeById(id);
            
            return Result.OK("删除成功");
        } catch (Exception e) {
            log.error("删除附件失败", e);
            return Result.error("删除附件失败: " + e.getMessage());
        }
    }

    @Override
    public List<WmsRequisitionAttachment> getAttachmentsByRequisitionId(String requisitionId) {
        LambdaQueryWrapper<WmsRequisitionAttachment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmsRequisitionAttachment::getRequisitionId, requisitionId);
        queryWrapper.orderByDesc(WmsRequisitionAttachment::getCreateTime);
        return this.list(queryWrapper);
    }
} 